/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.seu.cose.hobbysharing.tool;

import java.io.IOException;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

/**
 *
 * @author zc
 */
public class XMLTranslator {
    public static String objToXML(Object obj) throws PropertyException, JAXBException, IOException{
        JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                        true);
        StringWriter stringWriter = new StringWriter();
        jaxbMarshaller.marshal(obj, stringWriter);
        stringWriter.close();
        return stringWriter.toString();
    }
}
